//
//  QYGDrollTipView.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/1.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGDrollTipView.h"
#import "NSTimer+Utility.h"

@interface QYGDrollTipView()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIButton *startBtn;
@property (nonatomic, strong) NSTimer *timer;
// 倒计时
@property (nonatomic, assign) NSInteger countDown;
@property (nonatomic, strong) UILabel *detailLabel;

@end


@implementation QYGDrollTipView

- (void)dealloc{
    
    NSLog(@"----QYGDrollTipView-销毁---");
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)setDrollState:(DrollTipState)drollState{
    
    _drollState = drollState;
    NSString *cancelStr;
    NSString *sureStr;
    NSString *imgStr;
    
    if(drollState == DrollTipStateFail){
        imgStr = @"droll_tip_fail_img";
        cancelStr = @"休息一下";
        sureStr = @"再抓一次";
        self.cancelBtn.hidden = NO;
        self.sureBtn.hidden = NO;
        self.startBtn.hidden = YES;
        self.detailLabel.hidden = YES;
    }else if(drollState == DrollTipStateSucceed){
        
        imgStr = @"droll_tip_sucs_img";
        cancelStr = @"继续抓";
        sureStr = @"去领取";
        self.cancelBtn.hidden = NO;
        self.sureBtn.hidden = NO;
        self.startBtn.hidden = YES;
        self.detailLabel.hidden = YES;
    }else if(drollState == DrollTipStateBalanceLack){
        
        imgStr = @"droll_tip_coin_img";
        cancelStr = @"先围观";
        sureStr = @"去充值";
        self.cancelBtn.hidden = NO;
        self.sureBtn.hidden = NO;
        self.startBtn.hidden = YES;
        self.detailLabel.hidden = YES;
    }else if(drollState == DrollTipStateCanStart){
        
        imgStr = @"droll_tip_start_img";
        self.cancelBtn.hidden = YES;
        self.sureBtn.hidden = YES;
        self.startBtn.hidden = NO;
        self.detailLabel.hidden = NO;
    }
    
    [self.cancelBtn setTitle:cancelStr forState:UIControlStateNormal];
    [self.sureBtn setTitle:sureStr forState:UIControlStateNormal];
    self.imgView.image = [UIImage imageNamed:imgStr];
    [self.imgView sizeToFit];
    
    [self startTimerWithState:drollState];
    
}
- (void)startTimerWithState:(DrollTipState)drollState{
    
    if(drollState == DrollTipStateBalanceLack) return;
    self.countDown = 5;
    WS(weakSelf);
    self.timer = [NSTimer cm_scheduledTimerWithTimeInterVal:1 block:^{
        [weakSelf showCountTime];
    } repeats:YES];
    [self.timer fire];
}
- (void)showCountTime{
    
    if (self.countDown <= 0) {
        [self.timer invalidate];
        self.timer = nil;
        if(self.hiddenBlock){
            self.hiddenBlock();
        }
    }
    if(self.drollState == DrollTipStateFail){
        NSString *sureStr = [NSString stringWithFormat:@"再抓一次(%ld)",self.countDown];
       [self.sureBtn setTitle:sureStr forState:UIControlStateNormal];
    }else if(self.drollState == DrollTipStateSucceed){
        
        NSString *cancelStr = [NSString stringWithFormat:@"继续抓(%ld)",self.countDown];
        [self.cancelBtn setTitle:cancelStr forState:UIControlStateNormal];
        
    }else if(self.drollState == DrollTipStateCanStart){
        self.detailLabel.text = [NSString stringWithFormat:@"%ld秒内不开始游戏自动退出哦",self.countDown];
    }
    
    self.countDown--;
}

#pragma mark target
- (void)cancelBtnClick{
    
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    if(self.cancelBlock){
        self.cancelBlock(self.drollState);
    }
}

- (void)sureBtnClick{
    
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    if(self.suerBlock){
        self.suerBlock(self.drollState);
    }
}

- (void)startBtnBtnClick{
    
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    if(self.startBlock){
        self.startBlock();
    }
}
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        
        [self initUI];
    }
    return self;
}

#pragma mark setUI
- (void)initUI{
    
    [self addSubview:self.detailLabel];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.sureBtn];
    [self addSubview:self.startBtn];
    [self addSubview:self.imgView];
    
}
- (void)setFrame:(CGRect)frame{
    frame.size.width = ScaleW(230);
    frame.size.height = ScaleW(240);
    [super setFrame:frame];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.cancelBtn.x = 0;
    self.cancelBtn.y = self.height - self.cancelBtn.height;
    
    self.sureBtn.x = self.width - self.sureBtn.width;
    self.sureBtn.y = self.cancelBtn.y;
    
    self.detailLabel.x = 0;
    self.detailLabel.y = self.height - self.detailLabel.height;
    self.detailLabel.width = self.width;
    
    self.startBtn.x = (self.width - self.startBtn.width) * 0.5;
    self.startBtn.y = self.detailLabel.y - self.startBtn.height - ScaleH(10);
    
    self.imgView.centerX = self.centerX;
    self.imgView.y = 0;
    
}

- (UIImageView *)imgView{
    if(!_imgView){
        _imgView = [[UIImageView alloc] init];
    }
    return _imgView;
}

- (UIButton *)startBtn{
    
    if(!_startBtn){
        _startBtn = [SGUtil newButtonWithTitleFont:ScaleFont(14) title:@"进入游戏" titleColor:[UIColor colorWithHex:@"ffffff"]];
        [_startBtn viewWithcornerRadius:4 borderWidth:0 borderColor:nil backgroundColor:[UIColor colorWithHex:@"ffb52b"]];
        [_startBtn addTarget:self action:@selector(startBtnBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _startBtn.size = CGSizeMake(ScaleW(96), ScaleW(38));
        
    }
    return _startBtn;
}

- (UIButton *)sureBtn{
    
    if(!_sureBtn){
        _sureBtn = [SGUtil newButtonWithTitleFont:ScaleFont(14) title:@"" titleColor:[UIColor colorWithHex:@"ffffff"]];
        [_sureBtn viewWithcornerRadius:4 borderWidth:0 borderColor:nil backgroundColor:[UIColor colorWithHex:@"ffb52b"]];
        [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.size = CGSizeMake(ScaleW(96), ScaleW(38));
        
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn{
    
    if(!_cancelBtn){
        _cancelBtn = [SGUtil newButtonWithTitleFont:ScaleFont(14) title:@"" titleColor:[UIColor colorWithHex:@"50509a"]];
        [_cancelBtn viewWithcornerRadius:4 borderWidth:0 borderColor:nil backgroundColor:[UIColor qyg_mainColor]];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.size = CGSizeMake(ScaleW(96), ScaleW(38));
    }
    return _cancelBtn;
}
- (UILabel *)detailLabel{
    if(!_detailLabel){
        _detailLabel = [UILabel labelWithText:@"5秒内不开始游戏自动退出哦" setFont:ScaleFont(12) setTextColor:[UIColor colorWithHex:@"ffffff"] setTextAlignment:NSTextAlignmentCenter];
        _detailLabel.height = ScaleH(13);
        _detailLabel.numberOfLines = 1;
    }
    return _detailLabel;
}
@end
